package com.WISSC.serviceImpl;

import com.WISSC.bean.*;
import com.WISSC.mapper.OccupationImageMapper;
import com.WISSC.mapper.OccupationMapper;
import com.WISSC.service.OccupationImageService;
import com.WISSC.util.file.FileUpload;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.FileNotFoundException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 职业资格和图片关联表 服务实现类
 * </p>
 *
 * @author 张凯
 * @since 2019-05-06
 */
@Service
public class OccupationImageServiceImpl extends ServiceImpl<OccupationImageMapper, OccupationImage> implements OccupationImageService {
    @Autowired
    OccupationMapper occupationMapper;

    /**
     * @param file
     * @return 添加证书图片
     */
    @Override
    public Massage upload(MultipartFile file) {
        String url = "";
        try {
            url = FileUpload.fileUpload(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        OccupationImage occupationImage = new OccupationImage();
        occupationImage.setImageUrl(url);
        occupationImage.setCreateTime(new Date().getTime());
        Massage massage = new Massage();
        //上传成功 返回数据 返回本次上传的文件ID
        if (this.save(occupationImage)) {
            massage.setCode(0);
            massage.setMsg("上传成功");
            Map map = new HashMap<>();
            map.put("fileId", occupationImage.getId());
            map.put("url", url);
            System.out.println(occupationImage.getId());
            massage.setData(map);
            return massage;
        }
        massage.setCode(1);
        massage.setMsg("上传失败");
        return massage;
    }

    /**
     * @param qualifications
     * @return 资格证书url
     */
    @Override
    public List<OccupationImage> getOccupationImageUrl(Long id, Integer qualifications) {
        return baseMapper.getOccupationImageUrl(id, qualifications);
    }

    @Override
    public boolean removeByOccupationId(Long id) {
        if (baseMapper.remove(id)>0){
            return  true;
        }
        return false;
    }

}
